package com.gxwgy.wg2404.controller;

import com.gxwgy.wg2404.pojo.EventsPojo;
import com.gxwgy.wg2404.pojo.UsersPojo;
import com.gxwgy.wg2404.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 姚相国
 * @version 1.0
 * @className ActivityServlet
 * @date 2024/12/17 23:23
 */
@WebServlet({"/activity/list"})
public class ActivityServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String servletPath = request.getServletPath();
        String contextPath = request.getContextPath();

        //判断是否获取到了session
        HttpSession session = request.getSession(false);
        // 如果有session，就表示已登录，可以执行相关操作
        if (session != null && session.getAttribute("username") != null) {
            // 查询events表数据
            if ("/activity/list".equals(servletPath)) {
                doList(request, response);
            }

        } else {
            //如果没有登录，没有session就跳转到登录页面
            response.sendRedirect(contextPath);
        }
    }

    private void doUserList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<UsersPojo> userServlets = new ArrayList<>();

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getConnection();
            // 执行查询语句
            String sql = "select id, username, password, email, role, created_at, updated_at from users";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            // 遍历结果集
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String email = rs.getString("email");
                String role = rs.getString("role");
                String created_at = rs.getString("created_at");
                String updated_at = rs.getString("updated_at");

                UsersPojo usersPojo = new UsersPojo();
                usersPojo.setId(id);
                usersPojo.setEmail(email);
                usersPojo.setUsername(username);
                usersPojo.setPassword(password);
                usersPojo.setRole(role);
                usersPojo.setCreated_at(created_at);
                usersPojo.setUpdated_at(updated_at);

                userServlets.add(usersPojo);
            }

        } catch (SQLException e) {
            e.printStackTrace();
            throw new ServletException("数据库查询失败", e);
        }finally {
            // 关闭数据库连接
            DBUtil.close(conn, ps, rs);
        }
        request.setAttribute("users", userServlets);
        request.getRequestDispatcher("/administrator.jsp").forward(request,response);

    }

    private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 定义一个集合来存放所有的信息
        List<EventsPojo> eventsPojos = new ArrayList<>();

        // 连接数据库，查询所有
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            // 执行查询语句
            String sql = "select id, name, description, start_time, end_time, location, capacity, current_enrollment, status, created_at, updated_at from events";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery(sql);
            // 遍历结果集
            while (rs.next()) {
                String poll_id = rs.getString("id");
                String name = rs.getString("name");
                String description = rs.getString("description");
                String start_time = rs.getString("start_time");
                String end_time = rs.getString("end_time");
                String location = rs.getString("location");
                int capacity = rs.getInt("capacity");
                int current_enrollment = rs.getInt("current_enrollment");
                String status = rs.getString("status");
                String created_at = rs.getString("created_at");
                String updated_at = rs.getString("updated_at");

                // 以上零散的数据封装成java对象
                EventsPojo eventsPojo = new EventsPojo();
                eventsPojo.setId(poll_id);
                eventsPojo.setName(name);
                eventsPojo.setDescription(description);
                eventsPojo.setStart_time(start_time);
                eventsPojo.setEnd_time(end_time);
                eventsPojo.setLocation(location);
                eventsPojo.setCapacity(capacity);
                eventsPojo.setCurrent_enrollment(current_enrollment);
                eventsPojo.setStatus(status);
                eventsPojo.setCreated_at(created_at);
                eventsPojo.setUpdated_at(updated_at);

                // 收集所有信息
                eventsPojos.add(eventsPojo);
            }

        } catch (SQLException e) {
            e.printStackTrace();
            throw new ServletException("数据库查询失败", e);
        }finally {
            // 关闭数据库连接
            DBUtil.close(conn, ps, rs);
        }
        //将list集合放到请求域当中
        request.setAttribute("events", eventsPojos);
        //转发给list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }
}
